﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace JXT.PrimaryKey.Batman.Core.Configuration
{
    /// <summary>
    /// Memcached配置集合
    /// </summary>
    public class MemcachedSettingConfigurationElementCollection : ConfigurationElementCollection, IEnumerable<MemcachedSettingConfigurationElement>
    {
        public MemcachedSettingConfigurationElementCollection()
        {
            DefaultConnections = -1;
            MinConnections = -1;
            MaxConnections = -1;
            Timeout = -1;
        }

        /// <summary>
        /// 默认连接数
        /// </summary>
        [ConfigurationProperty("defaultConnections", IsRequired = false)]
        public int DefaultConnections
        {
            get { return (int)base["defaultConnections"]; }
            set { base["defaultConnections"] = value; }
        }

        /// <summary>
        /// 最小连接数
        /// </summary>
        [ConfigurationProperty("minConnections", IsRequired = false)]
        public int MinConnections
        {
            get { return (int)base["minConnections"]; }
            set { base["minConnections"] = value; }
        }

        /// <summary>
        /// 最大连接数
        /// </summary>
        [ConfigurationProperty("maxConnections", IsRequired = false)]
        public int MaxConnections
        {
            get { return (int)base["maxConnections"]; }
            set { base["maxConnections"] = value; }
        }

        /// <summary>
        /// 超时
        /// </summary>
        [ConfigurationProperty("timeout", IsRequired = false)]
        public int Timeout
        {
            get { return (int)base["timeout"]; }
            set { base["timeout"] = value; }
        }

        public new MemcachedSettingConfigurationElement this[string name]
        {
            get
            {
                return BaseGet(name) as MemcachedSettingConfigurationElement;
            }
            set
            {
                if (BaseGet(name) != null)
                {
                    base.BaseRemove(name);
                }
                BaseAdd(value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new MemcachedSettingConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((MemcachedSettingConfigurationElement)element).Name;
        }

        public new IEnumerator<MemcachedSettingConfigurationElement> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return (MemcachedSettingConfigurationElement)BaseGet(i);
            }
        }
    }
}
